/**
 * 
 */
package util;
import java.io.File;
import java.io.IOException;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import model.*;
/** 
 * @作者 Zouzr 
 * @创建日期 Dec 10, 2008 
 * @版本 V 1.0 
 */

public class ImportSpecial
{
		public static String importIntoSpecial(String filepath)
		{
			System.out.println("要导入的xls的路径为:"+filepath);
		    try {
		    	  String errormessage="";
			      //File file  = new File("C:\\Documents and Settings\\Administrator\\桌面\\teacher.xls"); 
		    	  File file  = new File(filepath); 
		    	  System.out.println("文件是否存在"+file.isFile());
		    	 
				  Workbook workbook = Workbook.getWorkbook(file);
				  Sheet sheet = workbook.getSheet(0); 
				 
				  int row=sheet.getRows();
				  int column=sheet.getColumns();
				  if(column!=3)
					  errormessage="Excel文件数据格式不对，无法正常导入";
				  
						for(int i=1;i<row;i++)
						{
							String id=sheet.getCell(0, i).getContents().trim();
							String specialname=sheet.getCell(1, i).getContents().trim();
							String specialdepartmentId=sheet.getCell(2,i).getContents().trim();
							
							Special special = new Special();
							try
							{
								special.setId(Integer.parseInt(id));
								special.setSpecialdepartmentId(Integer.parseInt(specialdepartmentId));
							}
							catch(Exception e)
							{
								if(!errormessage.equals(""))
									 errormessage="Excel文件数据格式不对，无法正常导入";//id列不为整数，则错误
							}
							if(!errormessage.equals(""))
								return errormessage;
							special.setSpecialname(specialname);
							
							ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
					        SpecialDAO specialdao=(SpecialDAO)context.getBean("SpecialDAOProxy");
					        specialdao.save(special);
					       
							System.out.println(id);
							System.out.println(specialname);
							
				      }
			      workbook.close();
			      
			}
			catch (Exception e)
			{
				//System.out.println(e.getMessage());
			}
			return "";
	    
		}
		
}
